package com.corpgovernment.travelstandard.impl;

import com.corpgovernment.api.travelstandard.service.ITravelStandardPostService;
import com.corpgovernment.api.travelstandard.vo.TravelStandardPostVo;
import com.corpgovernment.common.base.Page;
import com.corpgovernment.common.utils.ListUtils;
import com.corpgovernment.common.utils.ObjectUtils;
import com.corpgovernment.common.utils.PageUtils;
import com.corpgovernment.travelstandard.entity.db.TravelStandardPost;
import com.corpgovernment.travelstandard.mapper.TravelStandardPostMapper;
import com.ctrip.corp.obt.shard.context.PageContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class TravelStandardPostService extends TravelStandardBaseService implements ITravelStandardPostService {

    @Autowired
    private TravelStandardPostMapper travelStandPostMapper;

    @Override
    public void insert(TravelStandardPostVo model) {
        TravelStandardPost travelStandardPost = ObjectUtils.copyProperties(model, TravelStandardPost.class);
        travelStandPostMapper.insertSelective(travelStandardPost);
//        insertOperateLog(travelStandardPost);
    }


    @Override
    public int delete(Long id) {
        deleteOperateLog(String.valueOf(id));
        return travelStandPostMapper.deleteByPrimaryKey(id);
    }

    public int deleteByTsid(String tsid) {
        return 0;
    }

    @Override
    public int deleteByTsidAndTsType(Long tsid, String tsType) {
        return travelStandPostMapper.deleteByTsidAndTsType(tsid, tsType);
    }

    @Override
    public int update(TravelStandardPostVo model) {
        TravelStandardPost travelStandardPost = ObjectUtils.copyProperties(model, TravelStandardPost.class);
        try {
            TravelStandardPost oldValue = travelStandPostMapper.selectByPrimaryKey(travelStandardPost.getId());
            updateOperateLog(oldValue, travelStandardPost);
        } catch (Exception e) {
            log.error("更新操作获取查询失败,TravelStandardPostService.update");
        }
        return travelStandPostMapper.updateByPrimaryKeySelective(travelStandardPost);
    }

    @Override
    public TravelStandardPostVo get(Long id) {
        TravelStandardPost result = travelStandPostMapper.selectByPrimaryKey(id);
        if (result == null) {
            return null;
        }

        return ObjectUtils.copyProperties(result, TravelStandardPostVo.class);
    }

    @Override
    public Page page(TravelStandardPostVo model) {
        TravelStandardPost param = ObjectUtils.copyProperties(model, TravelStandardPost.class);
        PageContext.startPage(model.getPageNum(), model.getPageSize());
        List<TravelStandardPost> list = travelStandPostMapper.list(param);
        return PageUtils.optPageBy(list, TravelStandardPostVo.class);
    }

    @Override
    public void insertBatch(List<TravelStandardPostVo> model) {
        List<TravelStandardPost> list = ListUtils.copyList(model, TravelStandardPost.class);
        travelStandPostMapper.insertBatch(list);
        list.forEach(this::insertOperateLog);
    }

    @Override
    public List<TravelStandardPostVo> selectByTsTypeAndOrgId(String tsType, String orgId) {
        return ListUtils.copyList(travelStandPostMapper.selectByTsTypeAndOrgId(tsType, orgId), TravelStandardPostVo.class);
    }

    public List<TravelStandardPost> list(TravelStandardPostVo model) {
        List<TravelStandardPost> list = travelStandPostMapper.list(ObjectUtils.copyProperties(model, TravelStandardPost.class));
        return list;
    }

    public List<String> getJobNamesByTsIdAndTsType(Long tsId, String tsType) {
        return travelStandPostMapper.getJobNamesByTsIdAndTsType(tsId, tsType);
    }

}
